package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import android.view.View;

import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.StoreInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Tain_v2;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.MaintainPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IMaintain;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.AnimationUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DateUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;

import java.util.List;

import rx.Observable;

/**
 * 作者：Administrator on 2017/7/17 20:32
 * 邮箱：zhanghuaiha@gmail.com
 * 待维护门店
 */

public class MaintainActivity extends BaseActivity<IMaintain,MaintainPresenter> implements IMaintain{
    private TextView sort,page_tv;
    private ImageView back,bg,main_map;
    private RecyclerView recyclerView;
    private TextView list_yet,list_not,list_yi,main_dbl;
    private static final int NEXT = 101;
    private ImageView ts,main_shou;
    private int cont= 1000;
    private TextView yesterday,today,tomorrow;
    public boolean flag = false;
    private SwipeRefreshLayout main_sw;
    private FrameLayout main_yd;
    private EditText yet_search;
    private TextView yet_search_tv;

    private String time1 = DateUtils.getTime(System.currentTimeMillis());
    private int day = -1;


    public void initView() {
        sort = (TextView) findViewById(R.id.maintain_sort);
        back = (ImageView) findViewById(R.id.maintain_back);
        bg = (ImageView) findViewById(R.id.bg);
        ts = (ImageView) findViewById(R.id.ts);
        main_shou = (ImageView) findViewById(R.id.main_shou);
        main_map = (ImageView) findViewById(R.id.main_map);
        recyclerView = (RecyclerView) findViewById(R.id.maintain_recycler);
        page_tv = (TextView) findViewById(R.id.maintain_tv);
        list_yet = (TextView) findViewById(R.id.list_yet);
        list_not = (TextView) findViewById(R.id.list_not);
        list_yi = (TextView) findViewById(R.id.list_yi);
        yesterday = (TextView) findViewById(R.id.yesterday);
        today = (TextView) findViewById(R.id.today);
        tomorrow = (TextView) findViewById(R.id.tomorrow);
        main_dbl = (TextView) findViewById(R.id.main_dbl);
        main_sw = (SwipeRefreshLayout) findViewById(R.id.main_sw);
        main_yd = (FrameLayout) findViewById(R.id.main_yd);
        yet_search = (EditText) findViewById(R.id.yet_search);
        yet_search_tv = (TextView) findViewById(R.id.yet_search_tv);

    }
    long time_long = 0;
    @Override
    public void initData() {
        String tain = App.SP.getString("tain", "2");
        if (tain.equals("1")) {
            main_yd.setVisibility(View.GONE);
        }else {
            AnimationUtil.tranTain(MaintainActivity.this,main_shou);
        }
        today.setText(DateUtils.getTime(System.currentTimeMillis(),"MM-dd")+"(今日)");
        initPopWindow();
        mPresenter.loadContacts();
        mPresenter.loadNetWork("0",DateUtils.getTime(System.currentTimeMillis()),"");

    }

    //后一天
    public void tomorrow(){
        cont++;
        time_long = DateUtils.getTime_long(DateUtils.getNextDay(System.currentTimeMillis(), "yyyy-MM-dd", 1001-cont));
        if (1001 - cont == 0) {
            yesterday.setText("今日");
        }else {
            yesterday.setText("前一天");
        }
        if (999 - cont == 0) {
            tomorrow.setText("今日");
        }else {
            tomorrow.setText("后一天");
        }
        if (DateUtils.getTotDay(time_long, "yyyy-MM-dd", 1).equals(DateUtils.getTime(System.currentTimeMillis()))) {
            today.setText(DateUtils.getTotDay(time_long,"MM-dd",1)+"(今日)");
            yesterday.setText("昨天");
            tomorrow.setText("明天");
            mPresenter.visibleIsgone(true);
        }else {
            mPresenter.visibleIsgone(false);
            today.setText(DateUtils.getTotDay(time_long,"MM-dd",1));
        }
        time1 = DateUtils.getTotDay(time_long,"yyyy-MM-dd",1);
        mPresenter.loadNetWork("0",DateUtils.getTotDay(time_long,"yyyy-MM-dd",1),"");
    }
    //前一天
    public void yesterday(){
        time_long = DateUtils.getTime_long(DateUtils.getNextDay(System.currentTimeMillis(), "yyyy-MM-dd", 1000-cont));
        if (1000 - cont == 0) {
            tomorrow.setText("今日");
        }else {
            tomorrow.setText("后一天");
        }
        yesterday.setText("前一天");
        if (DateUtils.getNextDay(time_long, "yyyy-MM-dd", 1).equals(DateUtils.getTime(System.currentTimeMillis()))) {
            today.setText(DateUtils.getNextDay(time_long,"MM-dd",1)+"(今日)");
            mPresenter.visibleIsgone(true);
            yesterday.setText("昨天");
            tomorrow.setText("明天");
        }else {
            mPresenter.visibleIsgone(false);
            today.setText(DateUtils.getNextDay(time_long,"MM-dd",1));
        }
        time1 = DateUtils.getNextDay(time_long,"yyyy-MM-dd",1);
        mPresenter.loadNetWork("0",DateUtils.getNextDay(time_long,"yyyy-MM-dd",1),"");
        cont--;
    }

    @Override
    public void initListener() {
        //引导页
        main_yd.setOnClickListener(v -> {
            App.EDIT.putString("tain","1");
            App.EDIT.commit();
            main_yd.setVisibility(View.GONE);
        });

        yesterday.setOnClickListener(v -> yesterday());
        tomorrow.setOnClickListener(v -> {
            if (flag) {
                return;
            }
            tomorrow();
        });
        back.setOnClickListener(v -> finish());

        //待维护
        list_yet.setOnClickListener(v -> {
//            mPresenter.loadNetWork("0",time1,"0");
            mPresenter.not("0");
            list_yet.setSelected(true);
            list_not.setSelected(false);
            list_yi.setSelected(false);
        });

        //已维护
        list_yi.setOnClickListener(v -> {
            mPresenter.yi("2");
            list_not.setSelected(false);
            list_yet.setSelected(false);
            list_yi.setSelected(true);
        });

        //维护成功
        list_not.setOnClickListener(v -> {
            mPresenter.yet("1");
//            mPresenter.loadNetWork("0",time1,"1");
            list_not.setSelected(true);
            list_yet.setSelected(false);
            list_yi.setSelected(false);
        });

        today.setOnClickListener(v -> {
          //  titlePopup.show(findViewById(R.id.maintain_title));
            String time = mPresenter.getTime();
            Intent intent = new Intent(MaintainActivity.this,DateActivity.class);
            intent.putExtra("day",day);
            intent.putExtra("new_date",time);
            startActivityForResult(intent,NEXT);
        });



        String message = "待维护店铺如何计算的：\r\n" +
                " 1、WiFi设备\n" +
                "    每台设备不足在线120分钟\r\n" +
                " 2、密码设备\n" +
                "    店铺内密码设备，使用率底于80%\r\n" +
                " 3、判定维护成功\n" +
                "   在以上2点基础上，全部WiFi设备必须被APP【扫码充电】功能使用过";
        //问号
        ts.setOnClickListener(v -> {
            DialogUtils.TSLeftDialog(MaintainActivity.this,message,true);
        });

        //搜索
        yet_search_tv.setOnClickListener(v -> {
            List<Tain_v2> date = mPresenter.getDate();
            mPresenter.searh(yet_search_tv.getText().toString(),date);
            if (yet_search_tv.getText().toString().equals("搜索")) {
                yet_search_tv.setText("取消");
            }else {
                yet_search_tv.setText("搜索");
            }
        });

        //搜索
        yet_search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容

                } else {
                    // 此处为失去焦点时的处理内容
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
                }
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(recyclerView.getApplicationWindowToken(), 0);
            }
        });

    }

    private void initPopWindow() {
        // 实例化标题栏弹窗
//        titlePopup = new MaintainPopup(this, ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT);
//        titlePopup.setItemOnClickListener(onitemClick);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == NEXT) {
            if (null != data) {
                time1 = data.getStringExtra("time");
                day = data.getIntExtra("day",-1);
                mPresenter.loadNetWork("0", time1,"");
                list_yet.setSelected(false);
                list_not.setSelected(false);
                String substring = time1.substring(5);

                long l = System.currentTimeMillis() - DateUtils.getTime_long(time1);
                long l1 = l / 1000 / 60 / 60 / 24;
                System.out.println(l1+"---"+substring);
                cont = 1000;
                cont = cont - (int)l1;
                if (time1.equals(DateUtils.getTime(System.currentTimeMillis()))) {
                    mPresenter.visibleIsgone(true);
                    today.setText(substring+"(今日)");
                    yesterday.setText("昨天");
                    tomorrow.setText("明天");
                }else if (time1.equals(DateUtils.getNextDay(System.currentTimeMillis(), "yyyy-MM-dd", 1))) {
                    //昨天
                    tomorrow.setText("今日");
                    yesterday.setText("前一天");
                    mPresenter.visibleIsgone(false);
                    today.setText(substring);
                }else if (time1.equals(DateUtils.getTotDay(System.currentTimeMillis(), "yyyy-MM-dd", 1))) {
                    //明天
                    yesterday.setText("今日");
                    tomorrow.setText("后一天");
                    mPresenter.visibleIsgone(false);
                    today.setText(substring);
                }else{
                    mPresenter.visibleIsgone(false);
                    today.setText(substring);
                    yesterday.setText("前一天");
                    tomorrow.setText("后一天");
                }
            }
        }
    }

    @Override
    protected MaintainPresenter createPresenter() {
        return new MaintainPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_maintain;
    }

    @Override
    public RecyclerView getRv() {
        return recyclerView;
    }

    @Override
    public TextView getTV() {
        return page_tv;
    }

    @Override
    public TextView getYet() {
        return list_yet;
    }

    @Override
    public TextView getNot() {
        return list_not;
    }

    @Override
    public TextView getYi() {
        return list_yi;
    }

    @Override
    public TextView getSort() {
        return sort;
    }

    @Override
    public TextView getYesterday() {
        return yesterday;
    }

    @Override
    public TextView getTomorrow() {
        return tomorrow;
    }

    @Override
    public TextView getDbl() {
        return main_dbl;
    }

    @Override
    public EditText getSearch() {
        return yet_search;
    }


    @Override
    public ImageView getBg() {
        return bg;
    }

    @Override
    public ImageView getMap() {
        return main_map;
    }

    @Override
    public MaintainActivity getAc() {
        return MaintainActivity.this;
    }

    @Override
    public SwipeRefreshLayout getSw() {
        return main_sw;
    }
}
